//package com.mrzhou.rabbit.config;
//
//import com.mrzhou.core.tool.utils.StringUtil;
//import com.mrzhou.web.util.FileUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.BeansException;
//import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
//import org.springframework.beans.factory.support.BeanDefinitionRegistry;
//import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
//import org.springframework.data.elasticsearch.annotations.Document;
//import org.springframework.data.elasticsearch.annotations.Setting;
//import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
//import org.springframework.data.elasticsearch.core.IndexOperations;
//import org.springframework.stereotype.Component;
//
//import java.util.Arrays;
//import java.util.Optional;
//
///**
// *  在Bean构建完成后， 获取被@Document注解修饰的类
// *      分别创建索引
// * @author qiang.zhou
// */
//@Component
//@Slf4j
//public class ElasticsearchIndexConfiguration implements BeanDefinitionRegistryPostProcessor {
//
//    private BeanDefinitionRegistry beanDefinitionRegistry;
//
//    @Override
//    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
//        this.beanDefinitionRegistry = beanDefinitionRegistry;
//    }
//
//    @Override
//    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
//        // 通过BeanFactory获取Bean, 这里如果通过@Autowired获取不到Bean
//        ElasticsearchRestTemplate elasticsearchRestTemplate = configurableListableBeanFactory.getBean(ElasticsearchRestTemplate.class);
//
//        String[] names = configurableListableBeanFactory.getBeanNamesForAnnotation(Document.class);
//        Optional.ofNullable(names).ifPresent(beans -> {
//            final String[] defaultSetting = {FileUtil.loadSetting("es-settings/elasticsearch.json")};
//            Arrays.stream(names).forEach(var -> {
//                Class<?> clazz = configurableListableBeanFactory.getBean(var).getClass();
//                IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(clazz);
//                // 索引不存在时创建索引
//                if(!indexOperations.exists()) {
//                    Setting setting = clazz.getDeclaredAnnotation(Setting.class);
//                    if(null != setting && StringUtil.isNotBlank(setting.settingPath())) {
//                        defaultSetting[0] = FileUtil.loadSetting(setting.settingPath());
//                    }
//                    boolean flag = indexOperations.create(org.springframework.data.elasticsearch.core.document.Document.parse(defaultSetting[0]));
//                    log.info("创建{}索引成功", clazz.getName());
//                    if(flag) {
//                        // 设置索引的映射规则
//                        indexOperations.putMapping(indexOperations.createMapping(clazz));
//                        return;
//                    }
//                    log.error("创建{}索引失败： ", clazz.getName());
//                }
//                beanDefinitionRegistry.removeBeanDefinition(var);
//                log.warn(clazz.getName() + "已被移除， 请勿使用此类型bean");
//            });
//        });
//    }
//
//
//
//}
